﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Net.Mail;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;

namespace outREACH
{
    public partial class DevelopingCountries : System.Web.UI.Page
   {
       protected System.Web.UI.WebControls.TextBox CodeNumberTextBox;
       protected System.Web.UI.WebControls.Button SubmitButton;
       protected System.Web.UI.WebControls.Label MessageLabel;

       // For generating random numbers.
       private Random random = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
            Button6.OnClientClick = "return popup('Terms_and_Conditions.aspx', 'Terms_and_Conditions')";
            //List<DevelopingCountries1> results = DevelopingCountries1.ListAll();
            //foreach (DevelopingCountries1 udc in results)
            //{
            //    DropDownList1.Items.Add(udc.developingCountries);
            //}

            if (!this.IsPostBack)
            {
                // Create a random code and store it in the Session object.
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }

        }

        private string GenerateRandomCode()
        {
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, this.random.Next(10).ToString());
            return s;
        }

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DCuser dc = new DCuser();
            dc.developingCountries = DropDownList1.SelectedValue.ToString();
            dc.developingCity = TextBox1.Text.ToString();
            dc.nameOfUser = TextBox2.Text.ToString();
            dc.userPhoneNo = TextBox3.Text;
            dc.userEmail = TextBox4.Text.ToString();
            dc.dcUsername = TextBox5.Text.ToString();
            dc.dcPassword = TextBox6.Text.ToString();
            dc.dcAddress = TextBox10.Text.ToString();
            dc.dcZipcode = TextBox9.Text.ToString();
            //DCuser.Insert(dc);
            //SEND EMAIL TO OUTRECH ABOUT NEW NGO
            if (this.CodeNumberTextBox0.Text == this.Session["CaptchaImageText"].ToString())
            {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(TextBox4.Text);
            message.To.Add(new MailAddress("outreach360@gmail.com"));
            message.Subject = "New Developing Country has signed up!";
            message.Body = "Dear Outreach, a new Developing Country has signed up to our Outreach." + "\n" + "\nThe name of the Developing Country is:" + " " + DropDownList1.SelectedValue.ToString() + "\n" + "Below are the details: \n Developing City: " + TextBox1.Text.ToString() + "\n Name Of User: " + TextBox2.Text.ToString() + " \n Contact No: " + TextBox3.Text.ToString() + "\n Email: " + TextBox4.Text.ToString() + "\n Address: " + TextBox10.Text.ToString();


            // SMTP Configuration
            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");
            // MAIL MESSAGE CONFIGURATION

            // SEND EMAIL
            smtp.Send(message);
            }
            else
            {
                this.MessageLabel.CssClass = "error";
                this.MessageLabel.Text = "You have enter the wrong code. Please try again!";
                this.CodeNumberTextBox.Text = "";
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }

            Response.Redirect("RedirectDCNGO.aspx");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (this.CodeNumberTextBox.Text != null)
            {
                this.CodeNumberTextBox.Text = "";
            }
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }

   

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            //List<DevelopingCountries1> list = DevelopingCountries1.GetDevelopingCountries();
            //DropDownList1.DataSource = list;
            //DropDownList1.DataBind();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {

        }

      
   
    }
}